project(stairspeedtest LANGUAGES CXX)
cmake_minimum_required(VERSION 3.4)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include/")
IF(NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Release)
ENDIF()
SET(CMAKE_CXX_STANDARD 17)
ADD_DEFINITIONS(-Wall -Wextra -Wno-unused-parameter -Wno-unused-result)

OPTION(USING_STD_REGEX "Use std::regex from C++ library instead of PCRE2." OFF)

INCLUDE(CheckCXXSourceCompiles)
CHECK_CXX_SOURCE_COMPILES(
"
#include<string>
void main(){std::to_string(0);}
" HAVE_TO_STRING)

INCLUDE(CheckSymbolExists)
CHECK_SYMBOL_EXISTS(MSG_NOSIGNAL "sys/socket.h" HAVE_MSG_NOSIGNAL)

IF(APPLE)
    ADD_DEFINITIONS(-D_MACOS)
ENDIF()

IF(HAVE_TO_STRING)
    ADD_DEFINITIONS(-DHAVE_TO_STRING)
ENDIF()

IF(HAVE_MSG_NOSIGNAL)
	ADD_DEFINITIONS(-DHAVE_MSG_NOSIGNAL)
ENDIF()

ADD_EXECUTABLE(stairspeedtest 
	src/confbuild.cpp
	src/geoip.cpp
	src/logger.cpp
	src/main.cpp
	src/md5.cpp
	src/misc.cpp
	src/multithread_test.cpp
	src/ntt.cpp
	src/printmsg.cpp
	src/processes.cpp
	src/renderer.cpp
	src/rulematch.cpp
	src/socket.cpp
	src/speedtestutil.cpp
	src/tcping.cpp
	src/webget.cpp
	src/webgui_wrapper.cpp
	src/webserver_libevent.cpp)
INCLUDE_DIRECTORIES(src)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR})

FIND_PACKAGE(PkgConfig REQUIRED)

SET(THREADS_PREFER_PTHREAD_FLAG ON)
FIND_PACKAGE(Threads REQUIRED)
TARGET_LINK_LIBRARIES(stairspeedtest ${CMAKE_THREAD_LIBS_INIT})

PKG_CHECK_MODULES(LIBEVENT libevent REQUIRED)
FIND_PATH(LIBEVENT_INCLUDE_DIR NAMES event.h PATHS ${LIBEVENT_INCLUDE_DIRS})
FIND_LIBRARY(LIBEVENT_LIBRARY NAMES event PATHS ${LIBEVENT_LIBRARY_DIRS})
LINK_DIRECTORIES(${LIBEVENT_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${LIBEVENT_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(stairspeedtest ${LIBEVENT_LIBRARY})

FIND_PACKAGE(CURL 7.54.0 REQUIRED)
LINK_DIRECTORIES(${CURL_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(stairspeedtest CURL::libcurl)
ADD_DEFINITIONS(-DCURL_STATICLIB)

FIND_PACKAGE(OpenSSL 1.1.0 REQUIRED)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(stairspeedtest ${OPENSSL_LIBRARIES})

FIND_PACKAGE(Rapidjson REQUIRED)
INCLUDE_DIRECTORIES(${RAPIDJSON_INCLUDE_DIRS})

FIND_PACKAGE(PNG REQUIRED)
INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(stairspeedtest ${PNG_LIBRARIES})
ADD_DEFINITIONS(${PNG_DEFINITIONS})

FIND_PACKAGE(Freetype REQUIRED)
INCLUDE_DIRECTORIES(${FREETYPE_INCLUDE_DIR_freetype2})
TARGET_LINK_LIBRARIES(stairspeedtest ${FREETYPE_LIBRARIES})

FIND_PACKAGE(PNGwriter 0.7.0 REQUIRED)
INCLUDE_DIRECTORIES(${PNGwriter_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(stairspeedtest PNGwriter::PNGwriter)

PKG_CHECK_MODULES(YAML_CPP yaml-cpp REQUIRED)
FIND_LIBRARY(YAML_CPP_LIBRARY NAMES yaml-cpp yaml-cppd PATHS ${YAML_CPP_LIBRARY_DIRS})
LINK_DIRECTORIES(${YAML_CPP_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${YAML_CPP_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(stairspeedtest ${YAML_CPP_LIBRARY})
ADD_DEFINITIONS(-DYAML_CPP_STATIC_DEFINE)

IF(USING_STD_REGEX STREQUAL "ON")
    ADD_DEFINITIONS(-DUSE_STD_REGEX)
ELSE()
    FIND_PACKAGE(PCRE2 REQUIRED)
    INCLUDE_DIRECTORIES(${PCRE2_INCLUDE_DIRS})
    TARGET_LINK_LIBRARIES(stairspeedtest ${PCRE2_LIBRARY})
    ADD_DEFINITIONS(-DPCRE2_STATIC)
ENDIF()

IF(WIN32)
	TARGET_LINK_LIBRARIES(stairspeedtest wsock32 ws2_32)
ELSE()
	INCLUDE(GNUInstallDirs)
	INSTALL(TARGETS stairspeedtest DESTINATION ${CMAKE_INSTALL_BINDIR})
ENDIF()
